package com.uenpay.im.ui.old.activity.attendance;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.base.BaseActivity;
import com.uenpay.im.callback.IDateDailogListener;
import com.uenpay.im.weight.MyDateDialog;
import com.uenpay.im.weight.listview.RefreshListView;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * @author SL
 * @created 2016-3-16 17:43:03
 * @copyRight http://www.qiyunxin.com
 * @function TODO
 */
public class AttendanceListActivity extends BaseActivity implements
		RefreshListView.IRefreshListViewListener {

	private AttendanceHandle mAttendanceHandle = new AttendanceHandle();
	private int page = 1;
	private int entid;
	private AttendanceListAdapter mAttendanceListAdapter = null;
	private RefreshListView listView;
	private TextView noData, selected_date, choose_date;
	private int selectedYear = -1;
	private int selectedMonth = -1;
	private int selectedDay = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IMApplication.getInstance().addActivity(this);
		setContentView(R.layout.activity_attendance_list_layout);
		entid = getIntent().getIntExtra("entid", -1);
		initView();
		initListener();
		backListener();
		loading.setVisibility(View.VISIBLE);
		getData();
	}

	@Override
	protected void initView() {
		((TextView) findViewById(R.id.title_textview))
				.setText(R.string.sign_in_and_out);
		listView = (RefreshListView) findViewById(R.id.discover_listview);
		loading = findViewById(R.id.loading);
		noData = (TextView) findViewById(R.id.no_data_tv);
		selected_date = (TextView) findViewById(R.id.selected_date);
		choose_date = (TextView) findViewById(R.id.choose_date);

		Calendar calendar = Calendar.getInstance();
		selectedYear = calendar.get(Calendar.YEAR);
		selectedMonth = calendar.get(Calendar.MONDAY)+1;
		selectedDay = calendar.get(Calendar.DAY_OF_MONTH);
		selected_date.setText(selectedYear + "-" + selectedMonth + "-" + selectedDay);
	}

	@Override
	protected void initListener() {
		listView.setPullLoadEnable(true);
		listView.setRefreshListViewListener(this);
		choose_date.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				chooseDate();
			}
		});
	}

	private void getData() {
		String date = selected_date.getText().toString();
		mAttendanceHandle.getAttendanceList(entid + "",
				IMApplication.getCustId(), date, date, page,
				new AttendanceHandle.IAttendanceListResultListener() {

					@Override
					public void onAttendanceListResult(int status, String msg,
							ArrayList<AttendanceEntity> arrayList) {
						loading.setVisibility(View.GONE);
						stopAll();
						if (status == HttpInvokeResultCode.RESULT_OK) {
							if (page == 1) {
								if (arrayList != null && arrayList.size() > 0) {
									if (mAttendanceListAdapter == null) {
										mAttendanceListAdapter = new AttendanceListAdapter(
												AttendanceListActivity.this,
												arrayList);
									} else {
										mAttendanceListAdapter
												.setData(arrayList);
									}
									listView.setAdapter(mAttendanceListAdapter);
									listView.setPullLoadEnable(true);
									listView.setVisibility(View.VISIBLE);
								} else {
									noData.setVisibility(View.VISIBLE);
									listView.setVisibility(View.GONE);
								}

							} else {
								if (arrayList != null && arrayList.size() > 0) {
									mAttendanceListAdapter.addData(arrayList);
								} else {
									listView.setPullLoadEnable(false);
								}
							}
						}
					}
				});
	}

	private void chooseDate() {

		MyDateDialog dateDialog = new MyDateDialog(selectedYear, selectedMonth-1,
				selectedDay,-1,-1, AttendanceListActivity.this,false,true,
				new IDateDailogListener() {

					@Override
					public void OnDailogChick(String year, String Month,
							String Day, String hour, String minute) {
						selected_date.setText(year + "-" + Month + "-" + Day);
						selectedYear =Integer.valueOf(year);
						selectedMonth =Integer.valueOf(Month);
						selectedDay =Integer.valueOf(Day);
						page = 1;
						getData();
					}
				});
		dateDialog.showDialog();
	}

	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}

	private void stopAll() {
		listView.stopRefresh();
		listView.stopLoadMore();
	}

	@Override
	public void onRefresh() {
		page = 1;
		getData();
	}

	@Override
	public void onLoadMore() {
		page++;
		getData();
	}
}
